package tech.xs.dao.impl;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate5.HibernateTemplate;
import org.springframework.stereotype.Repository;

import tech.xs.dao.ClaimVoucherDao;
import tech.xs.pojo.ClaimVoucher;
import tech.xs.pojo.Employee;

@Repository
public class ClaimVoucherDaoImpl implements ClaimVoucherDao {
	// 封装模版类对象
	@Autowired
	private HibernateTemplate hibernateTemplate;

	// 注入
	public void setHibernateTemplate(HibernateTemplate hibernateTemplate) {
		this.hibernateTemplate = hibernateTemplate;
	}

	@Override
	public List<ClaimVoucher> findlist(Employee employee, ClaimVoucher voucher, Date date1, Date date2, int pageNo,
			int pageSize) {
		String hql = null;
		if (employee.getSysPosition().getId() == 1) {
			hql = "from ClaimVoucher where creator.sn =  '" + employee.getSn() + "' ";
		} else if (employee.getSysPosition().getId() == 2) {
			hql = "from ClaimVoucher where creator.department.id = " + employee.getSysDepartment().getId()
					+ " and nextDeal.id ='" + employee.getSn() + "' ";
			if (voucher.getStatus() == null) {
				hql += " and status not in :status ";
			}
		} else if (employee.getSysPosition().getId() == 3) {
			hql = "from ClaimVoucher where nextDeal.id ='" + employee.getSn() + "' ";
			if (voucher.getStatus() == null && !"".equals(voucher.getStatus())) {
				hql += " and status not in :status ";
			}
		} else if (employee.getSysPosition().getId() == 4) {
			hql = "from ClaimVoucher where nextDeal.id = '" + employee.getSn() + "' ";
			if (voucher.getStatus() == null && !"".equals(voucher.getStatus())) {
				hql += " and status = '新创建' ";
			}
		}
		if (voucher.getStatus() != null && !"".equals(voucher.getStatus())) {
			hql += " and status = '" + voucher.getStatus() + "' ";
		}
		if (date1 != null) {
			String ss = new SimpleDateFormat("yyyy-MM-dd").format(date1);
			hql += " and createTime > to_date('" + ss + "','YYYY-MM-DD') ";
		}
		if (date2 != null) {
			String ee = new SimpleDateFormat("yyyy-MM-dd").format(date2);
			hql += " and createTime < to_date('" + ee + "','YYYY-MM-DD') ";
		}
		hql += " order by createTime desc";
		@SuppressWarnings({ "unchecked", "deprecation" })
		List<ClaimVoucher> claimVoucherList = (List<ClaimVoucher>) hibernateTemplate.find(hql);
		System.out.println(claimVoucherList);
		return claimVoucherList;
	}

	@SuppressWarnings({ "unchecked", "deprecation" })
	@Override
	public int count(Employee employee, ClaimVoucher voucher, Date date1, Date date2) {
		 String hql = null;
		 if (employee.getSysPosition().getId() == 1) {
	            hql = "from ClaimVoucher where creator.sn =  '" + employee.getSn() + "' ";
	        } else if (employee.getSysPosition().getId() == 2) {
	            hql = "from ClaimVoucher where nextDeal.id ='" + employee.getSn() + "' ";
	            if (voucher.getStatus() == null) {
	                hql += " and status not in ? ";
	            }
	        }  else if (employee.getSysPosition().getId() == 3) {
	            hql = "from ClaimVoucher where nextDeal.id ='" + employee.getSn() + "' ";
	            if (voucher.getStatus() == null && !"".equals(voucher.getStatus())) {
	                hql += " and status not in ? ";
	            }
	        }  else if (employee.getSysPosition().getId() == 4) {
	            hql = "from ClaimVoucher where nextDeal.id = '" + employee.getSn() + "' ";
	            if (voucher.getStatus() == null && !"".equals(voucher.getStatus())) {
	                hql += " and status = '已审批' ";
	            }
	        }
	        if (voucher.getStatus() != null && !"".equals(voucher.getStatus())) {
	            hql += " and status = '" + voucher.getStatus() + "' ";
	        }
	        if (date1 != null) {
	            String ss = new SimpleDateFormat("yyyy-MM-dd").format(date1);
	            hql += " and createTime > to_date('" + ss + "','YYYY-MM-DD') ";
	        }
	        if (date2 != null) {
	            String ee = new SimpleDateFormat("yyyy-MM-dd").format(date2);
	            hql += " and createTime < to_date('" + ee + "','YYYY-MM-DD') ";
	        }
	        hql += " order by createTime desc";
	        
	        List<Object> list = new ArrayList<>();
	        if (employee.getSysPosition().getId() != 1 && employee.getSysPosition().getId() != 4) {
	            if (voucher.getStatus() == null) {
	                list = (List<Object>) hibernateTemplate.find(hql, "新创建");
	            } else {
	                list = (List<Object>) hibernateTemplate.find(hql);
	            }
	        } else {
	            list = (List<Object>) hibernateTemplate.find(hql);
	        }
	        if (list.size() > 0) {
	            return list.size();
	        }
	        return 0;
	}

	@SuppressWarnings({ "unchecked", "deprecation" })
	@Override
	public List<String> selectStatus() {
		String hql = "select DISTINCT status from ClaimVoucher";
        return (List<String>) hibernateTemplate.find(hql);
	}

	@Override
	public void delete(ClaimVoucher voucher) {
		hibernateTemplate.delete(voucher);
	}

	@Override
	public void save(ClaimVoucher claimVoucher) {	
		 hibernateTemplate.save(claimVoucher);		
	}

	@Override
	public ClaimVoucher findById(long id) {
		String hql = "from ClaimVoucher where id=?0";
		@SuppressWarnings({ "unchecked", "deprecation" })
		List<ClaimVoucher> list=(List<ClaimVoucher>) hibernateTemplate.find(hql, id);
		return list.get(0);
	}

	@Override
	public void update(ClaimVoucher claimVoucher) {
		hibernateTemplate.update(claimVoucher);
	}

}
